package com.theeviljames.linearAlgebra;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.PriorityQueue;

import com.theeviljames.exceptions.LinearAlgebraException;

public class LinearEquation implements Comparable{
	
	//MUST CHANGE LETTERED VARIABLES TO A SERIES OF x I.E. x1,x2 ...
	//SHOULD KEEP THE VARIABLE STRING FOR PRINTING REASONS
	
	
	private double[] coefficients = new double[0];
	private double[] rhs = {0.0};
	private String labels;
	
	public LinearEquation() {
		// TODO Auto-generated constructor stub
	}
	
	public LinearEquation(double[] coefficients, String labels) throws LinearAlgebraException{
		setCoefficients(coefficients, labels);
	}
	
/*	public int getNoOfCoefficients(){
		int counter = 0;
		for(int i = 0; i < coefficients.length; i++)counter += (coefficients[i]!=0)?1:0;
		return counter;
	}
*/	
	public String getLabels() {
		return labels;
	}
	
	public double[] getRHS(){
		return rhs;
	}
	
	public void setRHS(double[] rhs){
		this.rhs = rhs;
	}
	
	public double[] getCoefficients() {
		return coefficients;
	}
	
	public double getCoefficient(String label){
		int index = labels.indexOf(label);
		return coefficients[index];
	}
	
	public void setCoefficients(double[] coefficients){
		this.coefficients = coefficients;
	}
	
	public void setCoefficients(double[] coefficients, String labels) throws LinearAlgebraException{
		if(coefficients.length!=labels.length())throw new LinearAlgebraException("There must be the same number of coefficients and labels");
		this.coefficients = coefficients;
		this.labels = labels;
	}
	
	public String toString(){
		String result = "";
		for(int i = 0; i < coefficients.length; i++){
			if(coefficients[i]!=0){
				result += coefficients[i] + "" + labels.charAt(i);
				result += (i!=(coefficients.length-1))?" + ":" = ";
			}
		}
		result += rhs;
		return result;
	}
	
	public int compareTo(Object arg0) {
		LinearEquation eq2 = (LinearEquation)arg0;
		for(int i = 0; i < coefficients.length; i++){
			if(coefficients[i]==0&&eq2.getCoefficients()[i]==0)continue;
			if(coefficients[i]!=0&&eq2.getCoefficients()[i]==0)return -1;
			if(coefficients[i]==0&&eq2.getCoefficients()[i]!=0)return 1;
		}
		return 0;
	}
	
	public static void main(String[] args){
		try{
			LinearEquation eq1 = new LinearEquation();
			eq1.setCoefficients(new double[]{1,2,3}, "xyz");
			LinearEquation eq2 = new LinearEquation();
			eq2.setCoefficients(new double[]{0,2,3}, "xyz");
			LinearEquation eq3 = new LinearEquation();
			eq3.setCoefficients(new double[]{0,0,3}, "xyz");
			PriorityQueue<LinearEquation> eqs = new PriorityQueue<LinearEquation>();
			eqs.offer(eq3);
			eqs.offer(eq2);
			eqs.offer(eq1);
			System.out.println("Let's have a peek> " + eqs.peek());
			//System.out.println(eqs.peek().getNoOfCoefficients());
			for(int i = 0; i < 3; i++)System.out.println(eqs.remove());
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
